package com.weiwei.diancan.view;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weiwei.diancan.R;

/**
 * Created by BMW on 2016/11/6.
 */
public class BeizhuDialog extends Dialog implements View.OnClickListener {

    private final TextView tv_cancel;
    private final TextView tv_update;
    private final EditText et_num;
    private final TextView tv_title;
    private final TextView tv_message;
    private Activity context;

    private OnUpdateListener listener;

    public TextView getTv_update() {
        return tv_update;
    }

    public TextView getTv_title() {
        return tv_title;
    }

    public TextView getTv_message() {
        return tv_message;
    }

    public EditText getEt_num() {
        return et_num;
    }

    public BeizhuDialog(Activity context) {
        super(context, R.style.share_dialog);
        this.context = context;
        View v = LayoutInflater.from(context).inflate(R.layout.beizhu_dialog, null);
        et_num = (EditText)v.findViewById(R.id.et_num);
        et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)){
                    tv_update.setTextColor(Color.parseColor("#777777"));
                }else{
                    tv_update.setTextColor(Color.parseColor("#181818"));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tv_cancel = (TextView)v.findViewById(R.id.tv_cancel);
        tv_update = (TextView)v.findViewById(R.id.tv_update);
        tv_title = (TextView)v.findViewById(R.id.tv_title);
        tv_message = (TextView)v.findViewById(R.id.tv_message);
        tv_cancel.setOnClickListener(this);
        tv_update.setOnClickListener(this);
        addContentView(v,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    public OnUpdateListener getListener() {
        return listener;
    }

    public void setListener(OnUpdateListener listener) {
        this.listener = listener;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                cancel();
                break;
            case R.id.tv_update:
                if(listener!=null){
                    listener.onClick(v,et_num.getText().toString());
                }
                break;
        }
    }

    public interface OnUpdateListener{
        void onClick(View v, String text);
    }
}
